package linkedList;

import stack_queue.Stack;

/**
 * @program: play-structure
 * @description: 使用链表实现栈,链表栈
 * @author: baichen
 **/
public class LinkedListStack<E> implements Stack<E> {       //实现 Stack 接口
    private LinkedList<E> list;     //私有链表对象lis

    public LinkedListStack() {
        list = new LinkedList<>();        //链表初始化
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.removeFirst();
    }

    //查看栈顶元素
    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack: top ");
        res.append(list);
        return res.toString();
    }

    public static void main(String[] args) {

        LinkedListStack<Integer> stack = new LinkedListStack<>();    //声明LinkedListStack的对象stack

        for (int i = 0; i < 5; i++) {    //向栈中压入5个元素
            stack.push(i);
            System.out.println(stack);
        }

        stack.pop();    //从栈中取出元素
        System.out.println(stack);
    }
}
